<?
require_once(dirname(__FILE__) . '/../../classes/module_interface.inc.php');
require_once(dirname(__FILE__) . '/../../classes/strings.inc.php');
require_once(dirname(__FILE__) . '/../../classes/browscap.inc.php');
require_once(dirname(__FILE__) . '/config.inc.php');

class LoggerModulePlatforms extends LoggerStrings implements LoggerModuleInterface
{
    protected $database;
    protected $uaParser;

    public function __construct(LoggerDatabase $database) 
    {
        $this->database = $database;
        $this->stringsTable = 'module_platforms_strings';
        $this->uaParser = new LoggerBrowscap();
    }

    public function PrepareParseSession()
    {
    }

    public function ParseHit(&$hit)
    {
        if (!isset($hit['modules']['popularity']))
            return false;

        $arPlatform = $this->uaParser->getPlatform($hit['source']['agent']);
        if (!is_array($arPlatform))
            return false;

        $platformId = $this->GetStringId($arPlatform['name'], $hit['source']['time']);

        $date       = '\'' . mysql_escape_string($hit['source']['time']) . '\'';
        $log        = intval($hit['modules']['popularity']['id']);
        $platform   = intval($platformId);

        $InsertSQL = 'INSERT INTO `[PREF]module_platforms` (`date`, `log`, `platform`) ';
        $InsertSQL .= 'VALUES('.$date.', '.$log.', '.$platform.')';
        $result = $this->database->Query($InsertSQL);
        if (!$result)
            trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);

        return true;
    }

    public function Archive()
    {
        $date = LoggerModulePlatformsConfig::GetArchiveDate();
        $dateEscaped = '\''.mysql_escape_string($date).'\'';

        $this->database->Query('LOCK TABLES `[PREF]module_platforms` WRITE, `[PREF]module_platforms_archive` WRITE');
        
        $ArchiveSQL = 'INSERT INTO `[PREF]module_platforms_archive` (`date`, `platform`, `count`) ';
        $ArchiveSQL .= 'SELECT `date`, `platform`, COUNT(*) FROM `[PREF]module_platforms` WHERE `date`<'.$dateEscaped.' ';
        $ArchiveSQL .= 'GROUP BY `date`, `platform`';
        $this->database->Query($ArchiveSQL);
        $DeleteSQL = 'DELETE FROM `[PREF]module_platforms` WHERE `date`<'.$dateEscaped;
        $this->database->Query($DeleteSQL);

        $this->database->Query('UNLOCK TABLES');
    }

    public function Purge()
    {
        $date = LoggerModulePlatformsConfig::GetPurgeDate();
        $dateEscaped = '\''.mysql_escape_string($date).'\'';

        $DeleteSQL = 'DELETE FROM `[PREF]module_platforms` WHERE `time`<'.$dateEscaped;
        $this->database->Query($DeleteSQL);
        $DeleteSQL = 'DELETE FROM `[PREF]module_platforms_archive` WHERE `date`<'.$dateEscaped;
        $this->database->Query($DeleteSQL);

        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_platforms`';
        $this->database->Query($OptimizeSQL);
        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_platforms_archive`';
        $this->database->Query($OptimizeSQL);

        $this->PurgeStrings($date);

        return true;
    }
}